package com.yq.day22._01string._01comparator;


import sun.security.x509.AuthorityKeyIdentifierExtension;

import java.util.Arrays;
import java.util.Comparator;

/**
 * Comparator实现排序
 *
 * @author 3307235355@qq.com
 * @since 2022/06/22 15:04
 */
public class DemoComparator {
    public static void main(String[] args) {
        Student s1 = new Student("长风", 22, 80);
        Student s2 = new Student("景天", 19, 90);
        Student s3 = new Student("李明", 19, 80);
        Student[] students = new Student[]{s1, s2, s3};
        System.out.println(Arrays.toString(students));
        // Arrays.sort(students);
        // Arrays.sort(students,new MyComparator());
        // Arrays.sort(students,(o1,o2)->o1.getAge()-o2.getAge());
        //使用匿名内部类
        Arrays.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {

                return o1.getAge()-o2.getAge();
            }
        });
        //lambda表达式
        // Arrays.sort(students,DemoComparator::com);
        System.out.println(Arrays.toString(students));

    }
    public static int com(Student s1,Student s2){
        if (s1.getAge()==s2.getAge()){
            return s2.getScore()-s1.getScore();
        }
        return s1.getAge()-s2.getAge();
    }
}

//自定义比较器
class  MyComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        return o1.getAge()-o2.getAge();
    }
}
class Student{
    private String name;
    private int age;
    private int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}' + "\n";
    }
}